<template>
  <div class="goods-image" v-if="imgarr">
    <div class="large" :style="[{ backgroundImage: `url(${imgarr[nowindex]})` }, imgobj]" v-show="show"></div>
    <div class="middle" ref="middle" @mouseenter="show = true" @mouseleave="show = false" @mousemove="move">
      <img :src="imgarr[nowindex]">
      <div class="layer" :style="coverobj" v-show="show" id="cover"></div>
    </div>
    <ul class="small">
      <li v-for="(item, key) in imgarr" :key="key" :class="{ active: nowindex == key }">
        <img :src="item" @mouseenter="nowindex = key">
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  props: ['imgarr'],
  data() {
    return {
      nowindex: 0,
      show: false, // 控制色块和大图显示隐藏
      coverobj: { top: '0', left: '0' },// 设置色块的位置
      imgobj: { backgroundPositionX: '0', backgroundPositionY: '0' } // 设置图像的位移
    }
  },
  mounted() {

  },
  methods: {
    move(e) {
      var mid = this.$refs.middle;
      var cover = document.getElementById("cover")
      var x = e.pageX - this.getLeft(mid) - cover.offsetWidth/2
      var y = e.pageY - this.getTop(mid) - cover.offsetHeight/2
      var XMAX = mid.offsetWidth - cover.offsetWidth
      var YMAX = mid.offsetHeight - cover.offsetHeight
      if (x <= 0) {
        x = 0
      } else if (x >= XMAX) {
        x = XMAX
      }
      if (y <= 0) {
        y = 0
      } else if (y >= YMAX) {
        y = YMAX
      }
      this.coverobj.left = x + "px"
      this.coverobj.top = y + "px"
      this.imgobj.backgroundPositionX = -(x * 2) + "px"
      this.imgobj.backgroundPositionY = -(y * 2) + "px"
    },
    getTop(obj) {
      var offset = obj.offsetTop;
      if (obj.offsetParent != null) {
        offset += this.getTop(obj.offsetParent); 
      }
      return offset;
    },
    getLeft(obj) {
      var offset = obj.offsetLeft;
      if (obj.offsetParent != null) {
        offset += this.getLeft(obj.offsetParent);
      }
      return offset;
    }
  }
}
</script>

<style lang="scss" scoped>
@import "@/assets/css/variables.scss";

.goods-image {
  width: 480px;
  height: 400px;
  position: relative;
  display: flex;
  z-index: 2;

  .large {
    position: absolute;
    top: 0;
    left: 412px;
    width: 400px;
    height: 400px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    background-repeat: no-repeat;
    background-size: 800px 800px;
    background-color: #f8f8f8;
  }

  .middle {
    width: 400px;
    height: 400px;
    background: #f5f5f5;
    position: relative;
    cursor: move;

    img {
      width: 100%;
      height: 100%;
    }

    .layer {
      width: 200px;
      height: 200px;
      background: rgba(0, 0, 0, 0.2);
      left: 0;
      top: 0;
      position: absolute;
    }
  }

  .small {
    width: 80px;

    li {
      width: 68px;
      height: 68px;
      margin-left: 12px;
      margin-bottom: 15px;
      cursor: pointer;

      img {
        width: 100%;
        height: 100%;
      }

      &:hover,
      &.active {
        border: 2px solid $xtxColor;
      }
    }
  }
}
</style>